package rabbit.flt.r2dbc.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rabbit.flt.r2dbc.test.User;
import rabbit.flt.r2dbc.test.service.R2dbcService;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/r2dbc")
public class R2DBCController {

    @Autowired
    protected R2dbcService r2dbcService;

    @PostMapping("/insert/{username}")
    public Mono<User> insert(@PathVariable("username") String username) {
        return r2dbcService.add(username);
    }

    @PostMapping("/select/{username}")
    public Mono<User> select(@PathVariable("username") String username) {
        return r2dbcService.select(username);
    }

    @PostMapping("/select/bySql/{username}")
    public Mono<User> selectBySql(@PathVariable("username") String username) {
        return r2dbcService.selectFirst(username);
    }

    @PostMapping("/update/{oldName}/{newName}")
    public Mono<Integer> update(@PathVariable("oldName") String oldName,
                                @PathVariable("newName") String newName) {
        return r2dbcService.update(oldName, newName);
    }

    @PostMapping("/multiAdd/{username}")
    public Mono<Long> multiAdd(@PathVariable("username") String username) {
        return r2dbcService.multiAdd(username);
    }

    @PostMapping("/errorSql")
    public Mono<Void> errorSql() {
        return r2dbcService.errorSql();
    }

    @PostMapping("/delete/{username}")
    public Mono<Integer> delete(@PathVariable("username") String username) {
        return r2dbcService.delete(username);
    }
}
